package cn.daliyTest.com.thread.processCommunication;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * TODO
 *
 * @author ：ljf
 * @date ：Created in 2021/6/24 11:17
 */
public class TestSyncByCountDownLatch {
    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        List<String> list = new ArrayList<>();
        Thread threadA = new Thread(()->{
            for (int i = 0; i <10 ; i++) {
                list.add("111");
                System.out.println("A add list.size:"+list.size());
                if(list.size() == 5){
                    countDownLatch.countDown();
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread threadB = new Thread(()->{
            while (true){
                if(list.size()!=5){
                    try {
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("b 开始了");
                break;
            }
        });

        threadB.start();
        threadA.start();

    }
}
